"""author: wenyao
   data: 2021/4/28
   project: devopscmdb
"""
# #列表推导式
# a = [1,2,3,4,5]
# b = [ x**2 for x in a if x % 2 == 0]
# print(b)
#
# #打印30以内 能被3整除的整数的平方
# result = [x**2 for x in range(1,31) if x%3==0]
#
# #100以内能被3整除的数，如果是一个偶数就返回0，如果是奇数就返回这个数
# b = [0 if x%2==0 else x for x in range(101)  if x%3==0]
#
# #嵌套推导式
# #找出列表中名字含有两个以上"e"的名字
# names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
#          ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva', 'Elven']]
#
# print([ name for lst in names for name in lst if name.count("e")>=2])

#字典推导式
# dict1 = {"a":2,"b":1}
# dict2 = { v:k  for k,v in dict1.items()}
# print(dict2)

#key转化成小写，并且合并他们的值，得到数据--> {"a":5,"b":3,"c":3}
# dict2 = {"a":2,"b":1,"A":3,"c":3,"B":2}
# print({ k.lower():dict2.get(k.lower(),0)+dict2.get(k.upper(),0) for k,v in dict2.items()})


#集合推导式
# lst = [1,2,-2,2,3]
# set_li = {i for i in lst}
# print(set_li)

##########
# q1 = ['a','ab','abc','abcd','abcde']
# q2 =  [i.upper()  for i in q1 if len(i)>=3]
# print(q2)
#
# tup = [ (i,j) for i in range(6) if i%2==0 for j in range(5) if j%2 !=0]
# print(tup)

#迭代器与生成器
#可迭代对象
#迭代器
#生成器

#可迭代对象
##实现了__iter__方法，并且该方法返回一个迭代器
# lst = [1,2,3]
# from collections import Iterable
# print(isinstance(lst,Iterable))
# print(isinstance(lst,str))
# print(dir(lst))

#可迭代对象
#容器类型都是可迭代对象
#打开文件files，sockets等等

###迭代器
#实现了__iter__方法和__next__方法
#__iter__返回自身
#__next__返回下一个值

# lst = [1,2,3,4]
# # lst2 = lst.__iter__()
# lst2 = iter(lst)
# for i in lst2:
#     print(i)
# #print(next(lst2)
# # print(lst2.__next__())
# # print(lst2.__next__())
# # print(lst2.__next__())
# print(dir(lst2))
# #print(dir(range(10)))

# lst = [1,2,3,4,5,6,7,8,9,10]
# #懒加载  惰性求值
# #需要时再生成
# range(10)
#
# result = range(10)
# i_result = iter(result)
#
# #list  set tuple extend
# lst = list(i_result)
# from collections import Iterable, Iterator
# print(isinstance(i_result, Iterator))
# print(isinstance(i_result, Iterable))
# print(lst)

#可迭代对象常使用的模块工具

#生成无限序列
# from itertools import count
# counter = count(10,2)
# print(counter, type(counter))
# print(next(counter))
# print(next(counter))
# print(next(counter))

#从一个有限序列生成无限序列
# from itertools import cycle
# days = cycle(["星期一","星期二","星期三","星期四"])
# print(next(days))
# print(next(days))
# print(next(days))
# print(next(days))
# print(next(days))
# print(next(days))
# print(next(days))


#生成器
#生成器是一种特殊的迭代器
#相比于迭代器 更加优雅
#生成器不需要自己手动去实现__iter__和__next__，只需要一个yield关键字

#使用生成器表达式实现30以内能被3整除的数
# print(list((x for x in range(31) if x % 3 == 0)))


#yield关键字
#包含yield关键字的函数就叫做生成器函数

#一个函数中有多个yield
#一旦遇到yield就保留当前状态，然后返回yield的值
# a = 10
# yield

# def get_content():
#     x = 8
#     yield x-1
#     y = 6
#     yield y
#     z = 3
#     yield z
#
# g = get_content()
# print(g,type(g))
# print(next(g))  #第一获取完数据之后遇到第一个yield就退出，保留这一次执行的位置
# print(next(g))  #第二次再次执行的时候会从上一次执行的地方再执行
# print(next(g))
#
# def func1():
#     count = 1
#     while 1:
#         yield count
#         count +=1
#
# f = func1()
# print(next(f))
# print(next(f))

#递归实现斐波拉契数列
#f(n) = f(n-1) + f(n-2)

# def fib(n):
#     if n in [1,2]:
#         return 1
#     return fib(n-1) + fib(n-2)
# print([fib(n) for n in range(1,11)])

#生成器来实现斐波拉契数列
# def fib():
#     prev, curr = 0 ,1
#     while 1:
#         yield curr
#         prev, curr = curr, curr+prev
# f = fib()
# print([ next(f) for x in range(10)])

#生成器的好处
#可以用更少的代码来实现效果
#相比于容器对象更加节省cpu 内存

# lst = [("a",1),("b",2)]
# dict1 = {"a":1,"b":2}

#大文件处理
# def read_file(path):
#     SIZE = 4096
#     with open(path,'rb') as f:
#         while True:
#             block = f.read(SIZE)
#             if block:
#                 yield block
#             else:
#                 return

#send数据
# def counter():
#     count = 1
#     while True:
#         val = yield count
#         print(f"val is {val}")
#         if val is not None:
#             # count = val
#             count += 1
#         else:
#             count += 1

# count = counter()
# print(next(count))
# print(next(count))
# count.send(10)  #返回一个新的值给yield count,
# print(next(count))

# #####close属性 关闭当前生成器，关闭之后不能再继续生成值
# count.close()
# print(next(count))

#yield from
#yield from iterable

# def f1():
#     yield range(10)
#
# it1 = f1()
# print([x for x in it1])
#
# def f2():
#     yield from range(10)
#     #等效于
#     # for item in range(10):
#     #     yield item
#
# it2 = f2()
# # print([x for x in it2])
# for x in it2:
#     print(x)

with open("test.txt","r") as f:
    a = f.readline(2)
    # a = f.readline(6)
    print(a)

